package ui.model.user;

import java.util.ArrayList;
import java.util.List;

import business.entity.User;
import business.entity.VerifyUser;

import ui.model.ModelErrorInfo;
import ui.model.ViewModel;

public class UserLoginViewModel extends ViewModel {

	private User user;
	private VerifyUser verifyUser;
	
	public UserLoginViewModel() {
		verifyUser = new VerifyUser();
		this.errorInfos = new ArrayList<ModelErrorInfo>();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * 增加出错的信息
	 * 
	 * @param field
	 *            出错的域
	 * @param msg
	 *            出错的消息
	 */
	public void addError(String field, String msg) {
		ModelErrorInfo info = new ModelErrorInfo();
		info.setErrAttName(field);
		info.setErrMsg(msg);
		errorInfos.add(info);
	}

	public void clearError() {
		if (this.errorInfos != null)
			this.errorInfos.clear();
	}

	@Override
	public List<ModelErrorInfo> verifyModel() {
		if (user == null)
			return null;
		if (!errorInfos.isEmpty())
			return errorInfos;
		verifyUser.setUser(user);

		String errorMsg = null;
		if ((errorMsg = verifyUser.verifyUserName()) != null) {
			addError("name", errorMsg);
		} else if ((errorMsg = verifyUser.verifyUserPasswd()) != null) {
			addError("passwd", errorMsg);
		}

		if (!errorInfos.isEmpty())
			return errorInfos;
		else
			return null;
	}

	private List<ModelErrorInfo> errorInfos = null;
}
